package com.hsc.cmsservice.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hsc.cmsservice.entity.CrmBanner;
import com.hsc.cmsservice.service.CrmBannerService;
import com.hsc.commonutils.R;
import com.hsc.servicebase.exceptionHandle.GuliException;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 首页banner表 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2022-10-15
 */
@RestController
@RequestMapping("/educms/banneradmin")
//@CrossOrigin //gateway解决了可以注掉
public class BannerAdminController {
    @Autowired
    private CrmBannerService bannerService;

    //分页查询banner
    @GetMapping("pageBanner/{page}/{limit}")
    public R pageBanner(@PathVariable long page , @PathVariable long limit){
        Page<CrmBanner> pageBanner = new Page<>(page,limit);
       bannerService.page(pageBanner, null);
        List<CrmBanner> list = pageBanner.getRecords();
        long total = pageBanner.getTotal();

        return R.ok().data("total",total).data("items",list);
    }
    //根据id查询banner
    @GetMapping("get/{id}")
    public R getById(@PathVariable String id){
        CrmBanner banner = bannerService.getById(id);
        return R.ok().data("item",banner);
    }
    //添加banner
    @PostMapping("addBanner")
    public R addBanner(@RequestBody CrmBanner banner){
        bannerService.save(banner);
        return R.ok();
    }
    //修改banner
    @PutMapping("update")
    public R updateById(@RequestBody CrmBanner banner){
        bannerService.updateById(banner);
        return R.ok();
    }
    //删除banner
    @ApiOperation(value = "删除banner")
    @DeleteMapping("remove/{id}")
    public R remove(@PathVariable String id){
        boolean b = bannerService.removeById(id);
        if(b){
            return R.ok();
        }else {
            throw new GuliException(20001,"删除banner失败");
        }
    }

}

